package jaspect.model;

import java.util.Arrays;
import java.util.List;

/**
 * @author Abin Baby
 */
public class AspectCode {

    private String injectionMode;
    private List<String> methodList;
    private String injectMethod;
    private String serviceClass;

    /**
     *
     * @return
     */
    public String getInjectMethod() {
        return injectMethod;
    }

    /**
     *
     * @return
     */
    public String getInjectionMode() {
        return injectionMode;
    }

    /**
     *
     * @return
     */
    public List<String> getMethodList() {
        return methodList;
    }

    /**
     *
     * @param mode
     * @param parameters
     */
    public void parser(String mode, String parameters) {
        injectionMode = mode;
        int start = parameters.indexOf("(");
        int end = parameters.indexOf(")");

        String methods = parameters.substring(start, end);

        methodList = Arrays.asList(methods);
        injectMethod = parameters.substring(parameters.indexOf("{"), parameters.lastIndexOf("}"));



    }

    /**
     *
     * @param mode
     * @param methods
     * @param aspectMethod
     */
    public void parser(String mode, String methods, String aspectMethod) {
        this.injectionMode = mode;
        this.injectMethod = aspectMethod;
        this.methodList = Arrays.asList(methods.split(","));
    }

    /**
     *
     * @param methodName
     * @param aspectMethod
     * @param serviceClassName
     * @return
     */
    public  String toJavaString(String methodName, String aspectMethod, String serviceClassName) {
        StringBuilder sb = new StringBuilder();
       
        sb.append("\n");
        sb.append("\t\t" + serviceClassName + " t = new " + serviceClassName + "();");
        sb.append("\n");


        sb.append("\t\tException ex;\n");
        sb.append("\t\tString methodName = ex.getStackTrace()[0].getMethodName();\n");
        aspectMethod = aspectMethod.substring(0, aspectMethod.indexOf("("));
        sb.append("\t\tt." + aspectMethod + "(" + this.getClass().getName() + "," + "methodName");

        if (aspectMethod.equals("error")) {
            sb.append(",e);");
        } else {
            sb.append(");");
        }
        sb.append("\n}\n");




        return sb.toString();
    }
}

